package com.qf.service;

import com.baomidou.mybatisplus.core.InjectorResolver;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.DtsSerachDto;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: shop-manage
 *
 * @author: 元
 * time: 2021/9/14 0014 20:44
 * description:
 */
@Service
public class StorageServiceImpl implements StorageService{

    @Autowired
    private DtsStorageMapper dtsStorageMapper;

    //服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户名称
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户秘钥
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //桶的名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;



    @Override
    public DtsStorage add(String name, String ext, Integer size, byte[] content) throws Exception{
        //使用随机字符串生成文件的key
        String key = UUID.randomUUID().toString();
        //将文件存储到阿里云中
        String url = AliyunOSSUtil.uploadImage(endpoint,accessKeyId,accessKeySecret,bucketName,key + ext, content);
        //将文件保存路径存储到mysql数据库的storage表中保存
        DtsStorage storage = new DtsStorage();
        //文件上传路径
        storage.setUrl(url);
   //补全数据
        //更新时间
        storage.setUpdateTime(new Date());
        //添加时间
        storage.setAddTime(new Date());
        //文件大小
        storage.setSize(size);
        //文件类型, 扩展名
        storage.setType(ext);
        //文件名
        storage.setName(name);
        storage.setKey(key + ext);
        storage.setDeleted(false);

        dtsStorageMapper.insert(storage);
        return storage;
    }

    @Override
    public int deleteById(Integer id) {
        int counts = dtsStorageMapper.deleteById(id);
        return counts;
    }

    @Override
    public int updateStoarge(DtsStorage dtsStorage) {
        dtsStorage.setUpdateTime(new Date());
        int rows = dtsStorageMapper.updateById(dtsStorage);
        return rows;
    }

    @Override
    public Map selectByPage(DtsSerachDto serachDto) {
        PageHelper.startPage(serachDto.getPage(),serachDto.getLimit());
        List<DtsStorage> dtsStorages = dtsStorageMapper.queryList(serachDto);
        PageInfo<DtsStorage> pageInfo = new PageInfo<>(dtsStorages);

        Map data = new HashMap();
        data.put("total", pageInfo.getTotal());
        data.put("items", pageInfo.getList());

        return data;
    }


}
